package gazzera.swing;

import gazzera.gui.*;
import gazzera.gui.messages.*;
import gazzera.swing.components.Box;
import gazzera.swing.utility.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ChatWindow extends JFrame
{
	final JLabel banner;
	final JTextArea text;
	final JTextField input;
	final String nickname;
	final String hash;
	final GuiInterface gui;
	final Gazzera gazzera;

	public ChatWindow(String nickname, final String hash, final GuiInterface gui, final Gazzera gazzera) throws HeadlessException
	{
		super(nickname);
		this.nickname = nickname;
		this.hash = hash;
		this.gui = gui;
		this.gazzera = gazzera;
		setSize(300, 400);
		setIconImage(new ImageIcon(ResourceLoader.get("icon.png")).getImage());
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		Box b = new Box(true);
		//banner = new JLabel("<html>"+nickname+"<br><small style=\"color:#303030\">"+hash+"</small></html>");
		banner = new JLabel(nickname+"@"+hash);
		banner.setMinimumSize(new Dimension(10, 20));
		b.add(banner, false);
		
		
		text = new JTextArea();
		text.setEditable(false);
		b.add(new JScrollPane(text), true);
		
		input = new JTextField();
		b.add(input, false);
		
		input.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				String s = input.getText();
				text.append("<You> "+s+"\n");
				input.setText("");
				gui.pass(new ChatMessage(hash, s));
			}
		});
		
		add(b);
		
		addWindowListener( new WindowAdapter() {
			   public void windowOpened( WindowEvent e ){
			       
			     }
			   public void windowClosing( WindowEvent e ){
				   gazzera.closeChatWindow(ChatWindow.this);
			     }
			   } ); 

		
		
		
	}

	
	
	
}
